package com.sojson.project.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.enums.core.EGeneralStatus;
import com.sojson.project.controller.param.verify.SpecsCateParamVerify;
import com.sojson.project.entity.po.SpecsCate;
import com.sojson.project.service.SpecsCateService;
import com.sojson.result.Result;

import io.swagger.annotations.Api;

/**
 * 规格表控制器
 * 
 * @author liu
 * @date 2021-03-22
 */
@Api(tags = "规格表管理")
@RestController
@RequestMapping(value = "/specsCate")
public class SpecsCateController {

    @Resource
    private SpecsCateService specsCateServiceImpl;

    /**
     * 单条插入
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/specsCate/insert')")
    public Object insert(@RequestBody SpecsCate specsCate) throws SQLException {
        // 校验参数
        Result<?> paramResult = SpecsCateParamVerify.insert(specsCate);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        specsCate.insertInit();
        specsCateServiceImpl.insert(specsCate);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    // @PostMapping("/insertList")
    public Object insertList(@RequestBody SpecsCate specsCate) throws SQLException {
        return Result.success(specsCateServiceImpl.insertList(specsCate));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteByDto")
    public Object deleteByDto(@RequestBody SpecsCate specsCate) throws SQLException {
        return Result.success(specsCateServiceImpl.deleteByDto(specsCate));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/countAll")
    public Object countAll() throws SQLException {
        int countAll = specsCateServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findAll")
    public Object findAll() throws SQLException {
        List<SpecsCate> findAll = specsCateServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    // @PostMapping("/countByDto")
    public Object countByDto(@RequestBody SpecsCate specsCate) throws SQLException {
        int countByDto = specsCateServiceImpl.countByDto(specsCate);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findByDto")
    @PreAuthorize("@ss.hasPermi('/specsCate/findByDto')")
    public Object findByDto(SpecsCate specsCate) throws SQLException {
        List<SpecsCate> findByDto = specsCateServiceImpl.findByDto(specsCate);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@ss.hasPermi('/specsCate/deleteById/{id}')")
    public Object deleteById(@PathVariable Integer id) throws SQLException {
        if (id == null) {
            return Result.success();
        }
        specsCateServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @DeleteMapping(value = "/deleteByIdList")
    public Object deleteByIdList(@RequestParam("ids") ArrayList<Integer> ids) throws SQLException {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(specsCateServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateById")
    public Object updateById(@RequestBody SpecsCate specsCate) throws SQLException {
        specsCateServiceImpl.updateById(specsCate);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticByIdList")
    public Object updateStaticByIdList(@RequestBody SpecsCate specsCate) throws SQLException {
        return Result.success(specsCateServiceImpl.updateStaticByIdList(specsCate));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangeByIdList")
    public Object updateChangeByIdList(@RequestBody SpecsCate specsCate) throws SQLException {
        return Result.success(specsCateServiceImpl.updateChangeByIdList(specsCate));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/specsCate/updatePartialById')")
    public Object updatePartialById(@RequestBody SpecsCate specsCate) throws SQLException {
        // 校验参数
        Result<?> paramResult = SpecsCateParamVerify.updatePartialById(specsCate);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        specsCate.updateInit();
        specsCate.setUpdateNull();
        specsCateServiceImpl.updatePartialById(specsCate);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticPartialByIdList")
    public Object updateStaticPartialByIdList(@RequestBody SpecsCate specsCate) throws SQLException {
        return Result.success(specsCateServiceImpl.updateStaticPartialByIdList(specsCate));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangePartialByIdList")
    public Object updateChangePartialByIdList(@RequestBody SpecsCate specsCate) throws SQLException {
        return Result.success(specsCateServiceImpl.updateChangePartialByIdList(specsCate));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findById/{id}")
    public Object findById(@PathVariable Integer id) throws SQLException {
        SpecsCate findById = specsCateServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findByIdList")
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<SpecsCate> findByIdList = specsCateServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    // @PostMapping("/findIdByDto")
    public Object findIdByDto(@RequestBody SpecsCate specsCate) throws SQLException {
        ArrayList<Integer> findIdByDto = specsCateServiceImpl.findIdByDto(specsCate);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @PostMapping("/clear")
    public Object clear() throws SQLException {
        specsCateServiceImpl.clear();
        return Result.success();
    }

    /**
     * 根据对象查询(包含商品规格明细)
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/list")
    @PreAuthorize("@ss.hasPermi('/specsCate/list')")
    public Object list(SpecsCate specsCate) throws SQLException {
        List<SpecsCate> list = specsCateServiceImpl.list(specsCate);
        return Result.success(list, list);
    }

    /**
     * 通过id查询(包含商品规格明细)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/getInfo/{id}")
    public Object getInfo(@PathVariable Integer id) throws SQLException {
        SpecsCate getInfo = specsCateServiceImpl.getInfo(id);
        return Result.success(getInfo);
    }

    /**
     * 通过id更新(局部更新)(启用,禁用)
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updateStatusById")
    @PreAuthorize("@ss.hasPermi('/specsCate/updateStatusById')")
    public Object updateStatusById(@RequestBody SpecsCate specsCate) throws SQLException {
        // 校验参数
        Result<?> paramResult = SpecsCateParamVerify.updateStatusById(specsCate);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        SpecsCate speCate = new SpecsCate();
        speCate.updateInit();
        speCate.setId(specsCate.getId());
        speCate.setStatus(specsCate.getStatus());
        specsCateServiceImpl.updatePartialById(speCate);
        return Result.success();
    }

    /**
     * 根据对象查询
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findAppByDto")
    public Object findAppByDto(SpecsCate specsCate) throws SQLException {
        specsCate.setStatus(EGeneralStatus.NORMAL.getCode());
        List<SpecsCate> findByDto = specsCateServiceImpl.findByDto(specsCate);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 根据对象查询(包含商品规格明细)
     * 
     * @param specsCate
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/listApp")
    public Object listApp(SpecsCate specsCate) throws SQLException {
        specsCate.setStatus(EGeneralStatus.NORMAL.getCode());
        List<SpecsCate> list = specsCateServiceImpl.list(specsCate);
        return Result.success(list, list);
    }

}